package chapter1;

import java.util.List;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FindAllLinks {
	//Define driver a member variable
	WebDriver driver;
	String baseUrl = "http://dl.dropbox.com/u/55228056/Locators.html";
	@FindBy(tagName = "a") List <WebElement> links;
	
	 @BeforeTest
	  public void beforeTest()  {
		 //Open browser
		  WebDriver driver = new FirefoxDriver();
		  //Go to the website   test
		  driver.get(baseUrl);
		  //Assign the local driver handler to the member variable
		  //Use this keyword to avoid initiate a page twice (during PageFactory initiation)
		  this.driver=driver;
	  }

	 @AfterTest
	  public void afterTest() {
		 driver.close();
	  }
  @Test
  public void f() {
	  
	  //http://stackoverflow.com/questions/5186214/why-can-you-instantiate-a-class-within-its-definition
	  //Initiate a page with PageFactory within the class definition to use one class for test.
	  
	  FindAllLinks findAllLinks = PageFactory.initElements(driver, FindAllLinks.class);
	  
	//Get all the links displayed on Page
	  //List<WebElement> links = driver.findElements(By.tagName("a"));
	  //Verify there are ten links displayed on the page
	  Assert.assertEquals(10, findAllLinks.links.size());
	  //Iterate though the list of links and print
	  //target for each link
	  for(WebElement link : findAllLinks.links)
	  {System.out.println(link.getAttribute("href"));}
	
  }
 

}
